# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
from mindspore import dtype as mstype
from .transforms import DataAugmentationDINO_Cluster, copy
import mindspore.dataset as ds
from mindspore.dataset.vision import Inter
from mindspore.communication import get_rank, get_group_size
import mindspore.dataset.vision as C
import mindspore.dataset.transforms as C2


# 获取CC12M数据集
def get_cc12m_dataset(dataset_path, columns_list, num_parallel_workers, shuffle, batch_size):
    rank_id = get_rank()
    rank_size = get_group_size()
    cc12m_dataset = ds.MindDataset(dataset_path,
                                   columns_list=columns_list,
                                   num_parallel_workers=num_parallel_workers,
                                   shuffle=shuffle,
                                   num_shards=rank_size,
                                   shard_id=rank_id,
                                   num_samples=2048,
                                   )
    transform = DataAugmentationDINO_Cluster(global_crops_scale=(0.25, 1.0),
                                             local_crops_scale=(0.05, 0.25),
                                             local_crops_number=10)

    cc12m_dataset = cc12m_dataset.map(input_columns=["image", "token"], operations=transform,
                                      output_columns=["crops1", "crops2", "token"],
                                      column_order=["crops1", "crops2", "token"])

    # cc12m_dataset = cc12m_dataset.project(["crops1", "crops2", "token"])

    # print(cc12m_dataset.get_dataset_size())

    cc12m_dataset = cc12m_dataset.batch(batch_size)

    return cc12m_dataset


def get_dataset(dataset_path, batch_size):
    norm_mean = (0.48145466, 0.4578275, 0.40821073)
    norm_std = (0.26862954, 0.26130258, 0.27577711)
    norm_mean_2 = tuple(map(lambda x: x * 255, norm_mean))
    norm_std_2 = tuple(map(lambda x: x * 255, norm_std))
    val_dataset = ds.ImageFolderDataset(dataset_path, num_parallel_workers=4)
    val_dataset = val_dataset.map(
        [C.Decode(),
         C.Normalize(mean=norm_mean_2, std=norm_std_2),
         C.Resize(224, Inter.BICUBIC),
         C.CenterCrop(224),
         C.HWC2CHW(),
         C2.TypeCast(mstype.float32)],
        input_columns=["image"])
    val_dataset = val_dataset.batch(batch_size)
    return val_dataset
